/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package action.Profile;

import Database.ProfileDAO;
import Models.Profile;
import Models.User;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.validator.annotations.FieldExpressionValidator;
import com.opensymphony.xwork2.validator.annotations.RegexFieldValidator;
import com.opensymphony.xwork2.validator.annotations.RequiredStringValidator;
import com.opensymphony.xwork2.validator.annotations.StringLengthFieldValidator;
import com.opensymphony.xwork2.validator.annotations.VisitorFieldValidator;
import java.util.Date;
import java.util.Map;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;

/**
 *
 * @author trang_000
 */
@Namespace("/Profile")
public class ProfileAction extends ActionSupport implements ModelDriven<Profile> {

    private Profile profile = new Profile();
    private ProfileDAO profileDAO = new ProfileDAO();
    Map session = ActionContext.getContext().getSession();

    public ProfileAction() {
    }

    @Override
    @Action(value = "register", results = {
        @Result(name = "success", location = "/member/login.jsp"),
        @Result(name = "input", location = "/Profile/AddProfilePage.jsp")
    })
    public String execute() throws Exception {
        String result;
        User user = (User) session.get("user");
        if (user != null) {
            profile.setUser(user);
        }
        if (profileDAO.InsertProfile(profile)) {
            addActionMessage("Register Successfully!");
            result = SUCCESS;
        } else {
            addActionError("Register Failed (due to existed username or email).");
            result = INPUT;
        }

        return result;
    }

    @Override
//    @VisitorFieldValidator(message = "", appendPrefix = false)
    public Profile getModel() {
        return profile;
    }
}
